<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

    <title>Boost C++ Libraries</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            height: 100%;
        }

        .container {
            width: 800px;
            margin: 0px auto;
            margin-top: 15px;
        }

        .container .search {
            width: 100%;
            height: 52px;
        }

        .container .search input {
            float: left;
            width: 600px;
            height: 50px;
            border: 1px solid black;
            border-right: none;
            padding-left: 10px;
            color: black;
            font-size: 16px;
        }

        .container .search button {
            float: left;
            width: 150px;
            height: 52px;
            background-color: #4e6ef2;
            color: #FFF;
            font-size: 19px;
            font-family:Georgia, 'Times New Roman', Times, serif;
        }

        .container .result {
            width: 100%;
        }

        .container .result .item {
            margin-top: 15px;
        }

        .container .result .item a {
            display: block;
            text-decoration: none;
            font-size: 20px;
            color: #4e6ef2;
        }
        .container .result .item a:hover {
            text-decoration: underline;
        }
        .container .result .item p {
            margin-top: 5px;
            font-size: 16px;
            font-family:'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
        }

        .container .result .item i{
            display: block;
            font-style: normal;
            color: green;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="search">
            <input type="text" placeholder="请输入搜索内容">
            <button onclick="Search()">搜索一下</button>
        </div>
        <div class="result">
            <!-- 通过后端返回数据动态生成 -->
        </div>
    </div>
    <script>
        function Search(){
            let query = $(".container .search input").val();
            console.log("query = " + query); 
            if(query == '' || query == null)
            {
                document.write("搜索内容不能为空!!!");

                return;
            }

            $.ajax({
                type: "GET",
                url: "/s?word=" + query,
                success: function(data){
                    console.log(data);
                    BuildHtml(data);
                }
            });
        }

        function BuildHtml(data){
            if(data == '' || data == null)
            {
                let query = $(".container .search input").val();
                document.write("很抱歉，搜索内容：");
                document.write(query);
                document.write(" 没有找到");
                
                return;
            }

            let result_label = $(".container .result");
            result_label.empty();
            for( let elem of data){
                let a_label = $("<a>", {
                    text: elem.title,
                    href: elem.url,
                    target: "_blank"
                });
                
                let p_label = $("<p>", {
                    text: elem.desc
                });
                let i_label = $("<i>", {
                    text: elem.url
                });
                let div_label = $("<div>", {
                    class: "item"
                });

                a_label.appendTo(div_label);
                p_label.appendTo(div_label);
                i_label.appendTo(div_label);
                div_label.appendTo(result_label);
            }
        }
    </script>
</body>
</html>
